package com.flyaudio.xiaopengautopilot.activity;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RemoteViews;

import com.flyaudio.xiaopengautopilot.R;
import com.flyaudio.xiaopengautopilot.constant.JSONKeyName;
import com.flyaudio.xiaopengautopilot.presenter.IVehicleModule;
import com.flyaudio.xiaopengautopilot.util.JSONUtil;
import com.flyaudio.xiaopengautopilot.util.L;
import com.flyaudio.xiaopengautopilot.util.MySharedPreferences;

public class WidgetProvider extends AppWidgetProvider {
    private final String TAG = "WidgetProvider";

    /**
     * 每次窗口小部件被更新都调用一次该方法
     */
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        L.d(TAG, "onUpdate");
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_vehicle_layout);
        remoteViews.setOnClickPendingIntent(R.id.widget_vehicle_ll, getOpenPendingIntent(context));
        updateView(context, appWidgetManager, appWidgetIds, remoteViews);
    }

    private void updateView(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, RemoteViews remoteViews) {
        L.d(TAG, "updateView");
        setRemoteViews(context, remoteViews);
        for (int appWidgetId : appWidgetIds) {
            appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
        }
    }

    @SuppressLint("StringFormatInvalid")
    private void setRemoteViews(Context context, RemoteViews remoteViews) {
 //       String sPower = context.getResources().getString(R.string.widget_noinfo);
//        if (DataInfo.getInstance().getDataParamerInfo(ModuleKey.BMS_SOC) != null) {
//            String sTemp = context.getResources().getString(R.string.widget_vehicle_power);
//            int mSoc = (int) DataInfo.getInstance().getDataParamerInfo(ModuleKey.BMS_SOC);
//            sPower = String.format(sTemp,mSoc);
//        }
        String sTemp = context.getResources().getString(R.string.widget_vehicle_power);
        int mSoc = (int) JSONUtil.getInstance().getMileageJSON(JSONKeyName.BmsSoc, 0);
        String sPower = String.format(sTemp,mSoc);
        remoteViews.setTextViewText(R.id.widget_vehicle_power_tv, sPower);

//        String sMileage = context.getResources().getString(R.string.widget_noload_info);
//        if (DataInfo.getInstance().getDataParamerInfo(ModuleKey.BMS_EnduranceMileage) != null) {
//            String sTemp = context.getResources().getString(R.string.widget_vehicle_rechage);
//            int mEnduranceMileage = (int) DataInfo.getInstance().getDataParamerInfo(ModuleKey.BMS_EnduranceMileage);
//            sMileage = String.format(sTemp,mEnduranceMileage);
//        }

        String sMileageStr= context.getResources().getString(R.string.widget_vehicle_rechage);
        int mEnduranceMileage = (int)  JSONUtil.getInstance().getMileageJSON(JSONKeyName.EnduranceMileage, 0);
         String sMileage = String.format(sMileageStr,mEnduranceMileage);
        remoteViews.setTextViewText(R.id.widget_vehicle_rechage_tv, sMileage);

        MySharedPreferences.attachContext(context);
        String recent_10_100 = context.getResources().getString(R.string.widget_vehicle_recent_10_km);
        String recent_pc = String.format(context.getResources().getString(R.string.widget_vehicle_hundred_km_unit), 22.2);

        double pcVal = 22.2;
        if (MySharedPreferences.getPowerComsumptionKmSwitch()) {
            recent_10_100 = context.getResources().getString(R.string.widget_vehicle_recent_10_km);
            if (IVehicleModule.getInstance() != null) {
                pcVal = IVehicleModule.getInstance().getMileageData(10*1000);
            }
            recent_pc = String.format(context.getResources().getString(R.string.widget_vehicle_hundred_km_unit), pcVal);
        } else {
            recent_10_100 = context.getResources().getString(R.string.widget_vehicle_recent_100_km);
            if (IVehicleModule.getInstance() != null) {
                pcVal = IVehicleModule.getInstance().getMileageData(100*1000);
            }
            recent_pc = String.format(context.getResources().getString(R.string.widget_vehicle_hundred_km_unit), pcVal);
        }
        remoteViews.setTextViewText(R.id.widget_vehicle_recent_km_tv, recent_10_100);
        remoteViews.setTextViewText(R.id.widget_vehicle_power_consumption_tv, recent_pc);
    }

    /**
     * 获取 打开 MainActivity 的 PendingIntent
     */
    private PendingIntent getOpenPendingIntent(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        return pi;
    }

    /**
     * 接收窗口小部件点击时发送的广播
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        L.d(TAG, "onReceive action: " + intent.getAction());
        ComponentName componentName = new ComponentName(context, WidgetProvider.class);
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_vehicle_layout);
        setRemoteViews(context, remoteViews);
        AppWidgetManager awm = AppWidgetManager.getInstance(context);
        awm.updateAppWidget(componentName, remoteViews);
    }

    /**
     * 每删除一次窗口小部件就调用一次
     */
    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
    }

    /**
     * 当最后一个该窗口小部件删除时调用该方法
     */
    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
    }

    /**
     * 当该窗口小部件第一次添加到桌面时调用该方法
     */
    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
    }

    /**
     * 当小部件大小改变时
     */
    @Override
    public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
        super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
    }

    /**
     * 当小部件从备份恢复时调用该方法
     */
    @Override
    public void onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds) {
        super.onRestored(context, oldWidgetIds, newWidgetIds);
    }
}
